package
{
	import flash.display.FrameLabel;
	import flash.display.Sprite;
	import flash.events.Event;
	
	[SWF(width = "550", height = "600", frameRate = "60", backgroundColor = "0x000912")]
	public class Main extends Sprite
	{
		private static const TIME_TO_MOVE:Number = 860;
		private var _currentTimeToMove:Number;
		
		public var currentForm:Forma;
		
		public function Main()
		{			
			this._currentTimeToMove = TIME_TO_MOVE;
			this.currentForm = getRandomForm();
			for (var i:int = 0; i < 4; i++)
				this.addChild( this.currentForm.getBlock(i).toDisplay() );
			
			this.addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function update(e:Event):void
		{
			this._currentTimeToMove -= 1000 / this.stage.frameRate;
			if(this._currentTimeToMove <= 0)
			{
				trace("On time");
				for (var i:int = 0; i < 4; i++) 
					this.currentForm.getBlock(i).y += 1;	
				this._currentTimeToMove = TIME_TO_MOVE;
			}
		}
		
		private function getRandomForm():Forma
		{
			var chance:Number = Math.random();
			if(chance < 0.142)
				return new Forma(Forma.I);
			if(chance < 0.284)
				return new Forma(Forma.J);
			if(chance < 0.426)
				return new Forma(Forma.L);
			if(chance < 0.568)
				return new Forma(Forma.O);
			if(chance < 0.71)
				return new Forma(Forma.S);
			if(chance < 0.852)
				return new Forma(Forma.Z);
			else
				return new Forma(Forma.I);
		}
	}
}